<template>
  <div class="logincontainer">
    <el-form ref="addModel" class="loginForm" :model="addModel" :rules="rules" :inline="false" size="small">
      <el-form-item label="">
        <span class="loginTitle">xxxxx权限管理系统</span>
      </el-form-item>
      <el-form-item prop="username">
        <el-input v-model="addModel.username" placeholder="请输入用账户" />
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="addModel.password"
          :type="passwordVisible ? 'text' : 'password'"
          placeholder="请输入密码"
        >
          <template #suffix>
            <i
              :class="passwordVisible ? 'el-icon-view' : 'el-icon-view-off'"
              class="password-toggle-icon"
              @click="togglePasswordVisibility"
            />
          </template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-row :gutter="20">
          <el-col :span="12" :offset="0">
            <el-button class="mybtn" type="primary" @click="onSubmit">登录</el-button>
          </el-col>
          <el-col :span="12" :offset="0">
            <el-button class="mybtn">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      addModel: {
        username: 'admin',
        password: '123456',
        userType: ''
      },
      rules: {
        username: [
          {
            required: true,
            message: '请输入用户名称',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入用户密码',
            trigger: 'blur'
          }
        ]
      },
      passwordVisible: false // 控制密码显示/隐藏
    }
  },
  methods: {
    togglePasswordVisibility() {
      this.passwordVisible = !this.passwordVisible
    },
    onSubmit() {
      this.$refs.addModel.validate((valid) => {
        if (valid) {
          this.$store.dispatch('user/login', this.addModel).then(() => {
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.logincontainer {
  height: 100%;
  background-color: #fff;
  background-image: url('../../assets/login_bg.png');
  display: flex;
  align-items: center;
  justify-content: center;
  background-size: 100%;
  .loginForm {
    height: 350px;
    width: 450px;
    background: #fff;
    padding: 35px 20px;
    border-radius: 10px;
    .loginTitle {
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 24px;
      font-weight: 600;
      color: #409eff;
    }
    .mybtn {
      width: 100%;
    }
    .password-toggle-icon {
      cursor: pointer;
      font-size: 18px;
      color: #909399;
    }
  }
}
</style>
